package com.proton.carepatchtemp.activity;

import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.content.ContextCompat;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.Gravity;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.proton.carepatchtemp.R;
import com.proton.carepatchtemp.application.VCare;
import com.proton.carepatchtemp.base.BaseActivity;
import com.proton.carepatchtemp.constant.Codes;
import com.proton.carepatchtemp.constant.Extras;
import com.proton.carepatchtemp.ui.FlowLayout;
import com.proton.carepatchtemp.utils.eventbus.MainEvent;
import com.proton.carepatchtemp.utils.net.ReportCenter;
import com.proton.carepatchtemp.utils.ui.ToastUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class AddNotesActivity extends BaseActivity {

    private ImageButton mBtn_back;
    private TextView mTv_drug_treatment;
    private TextView mTv_physical_therapy;
    private TextView mTv_physical_condition;
    private FlowLayout mFlowLayout;
    private TextView mTv_save_note;
    private EditText mEt_note_conten;
    private String[] mDatas = new String[]{
            VCare.get().getString(R.string.note_drug_ibuprofen),
            VCare.get().getString(R.string.note_drug_paracetamol),
            VCare.get().getString(R.string.note_drug_dextro_ibuprofen_suppository),
            VCare.get().getString(R.string.note_drug_999_cold_medicine)
    };

    private int type = 0;

    private Long reportId;

    @Override
    public void dealEvent(MainEvent event) {
        switch (event.getId()) {
            case Codes.REPORT_ADD_NOTE:
                if (event.isSuccess()) {
                    ToastUtils.s(R.string.add_note_success);
                    finish();
                } else {
                    ToastUtils.e(R.string.add_note_failed);
                }
                break;
            default:
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_notes);

        if (!getIntent().hasExtra(Extras.REPORTID) || 0 == getIntent().getLongExtra(Extras.REPORTID, 0)) {
            ToastUtils.e(R.string.get_report_failed);
            finish();
        } else {
            reportId = getIntent().getLongExtra(Extras.REPORTID, 0);
        }

        bindViews();
        addView2FlowLayout();
        setListener();
        type = 1;
//        initViewPager();
//        mVp_notes_container.setAdapter(new NotesPagerAdapter(getSupportFragmentManager(),mFragments));
//        mVp_notes_container.addOnPageChangeListener(new NotesPagerChangeListener());
    }

    @Override
    public void setListener() {

        mBtn_back.setOnClickListener(view -> finish());

        mTv_drug_treatment.setOnClickListener(view -> {
            if (type == 1) {
                return;
            }

            mEt_note_conten.setHint(R.string.notes_hint_drug);
            mTv_drug_treatment.setTextSize(18);
            mTv_physical_therapy.setTextSize(14);
            mTv_physical_condition.setTextSize(14);
            mTv_drug_treatment.setTextColor(ContextCompat.getColor(this, R.color.blueDeep));
            mTv_physical_therapy.setTextColor(ContextCompat.getColor(this, R.color.gray));
            mTv_physical_condition.setTextColor(ContextCompat.getColor(this, R.color.gray));
            type = 1;
            mDatas = new String[]{
                    VCare.get().getString(R.string.note_drug_ibuprofen),
                    VCare.get().getString(R.string.note_drug_paracetamol),
                    VCare.get().getString(R.string.note_drug_dextro_ibuprofen_suppository),
                    VCare.get().getString(R.string.note_drug_999_cold_medicine)
            };
            addView2FlowLayout();
        });

        mTv_physical_therapy.setOnClickListener(view -> {
            if (type == 2) {
                return;
            }
            mEt_note_conten.setHint(R.string.notes_hint_phyT);
            mTv_drug_treatment.setTextSize(14);
            mTv_physical_therapy.setTextSize(18);
            mTv_physical_condition.setTextSize(14);
            mTv_drug_treatment.setTextColor(ContextCompat.getColor(this, R.color.gray));
            mTv_physical_therapy.setTextColor(ContextCompat.getColor(this, R.color.blueDeep));
            mTv_physical_condition.setTextColor(ContextCompat.getColor(this, R.color.gray));
            type = 2;
            mDatas = new String[]{
                    VCare.get().getString(R.string.note_phy_ice_bag),
                    VCare.get().getString(R.string.note_phy_warm_bath),
                    VCare.get().getString(R.string.note_phy_alcohol_sponge_bath),
                    VCare.get().getString(R.string.note_phy_feet_in_warm_water),
                    VCare.get().getString(R.string.note_phy_cooling_gel),
                    VCare.get().getString(R.string.note_phy_drink_water)
            };
            addView2FlowLayout();
        });

        mTv_physical_condition.setOnClickListener(view -> {
            if (type == 3) {
                return;
            }
            mEt_note_conten.setHint(R.string.notes_hint_phyC);
            mTv_drug_treatment.setTextSize(14);
            mTv_physical_therapy.setTextSize(14);
            mTv_physical_condition.setTextSize(18);
            mTv_drug_treatment.setTextColor(ContextCompat.getColor(this, R.color.gray));
            mTv_physical_therapy.setTextColor(ContextCompat.getColor(this, R.color.gray));
            mTv_physical_condition.setTextColor(ContextCompat.getColor(this, R.color.blueDeep));
            type = 3;
            mDatas = new String[]{
                    VCare.get().getString(R.string.note_con_coughed),
                    VCare.get().getString(R.string.note_con_dehydrated),
                    VCare.get().getString(R.string.note_con_twitchy),
                    VCare.get().getString(R.string.note_con_unconcious),
                    VCare.get().getString(R.string.note_con_general_weakness),
                    VCare.get().getString(R.string.note_con_lethargic),
                    VCare.get().getString(R.string.note_con_diarrheic),
                    VCare.get().getString(R.string.note_con_vomitive),
                    VCare.get().getString(R.string.note_con_snotty),
                    VCare.get().getString(R.string.note_con_inappetent),
                    VCare.get().getString(R.string.note_con_crying),
                    VCare.get().getString(R.string.note_con_cold_limbs)
            };
            addView2FlowLayout();
        });

        mTv_save_note.setOnClickListener(view -> {
            if (!TextUtils.isEmpty(mEt_note_conten.getText())) {
                ReportCenter.addComment(reportId + "", type, mEt_note_conten.getText().toString());

            } else {
                ToastUtils.w(R.string.content_not_empty);
            }
        });
    }

    private void bindViews() {
        mBtn_back = findViewById(R.id.btn_back);
        mTv_drug_treatment = findViewById(R.id.tv_drug_treatment);
        mTv_physical_therapy = findViewById(R.id.tv_physical_therapy);
        mTv_physical_condition = findViewById(R.id.tv_physical_condition);
        mFlowLayout = findViewById(R.id.flow_layout);
        mTv_save_note = findViewById(R.id.tv_save_note);
        mEt_note_conten = findViewById(R.id.et_note_content);
    }

    private void addView2FlowLayout() {
        mFlowLayout.removeAllViews();
        for (int i = 0; i < mDatas.length; i++) {
            final TextView view = new TextView(this);
            view.setText(mDatas[i]);
            view.setTextColor(Color.WHITE);
            view.setPadding(5, 5, 5, 5);
            view.setGravity(Gravity.CENTER);
            view.setTextSize(14);

            Random random = new Random();
            // 设置点击事件
            view.setOnClickListener(v -> mEt_note_conten.append(view.getText() + ";"));

            // 设置彩色背景
            GradientDrawable normalDrawable = new GradientDrawable();
            normalDrawable.setShape(GradientDrawable.RECTANGLE);
            normalDrawable.setCornerRadii(new float[]{8F, 8F, 8F, 8F, 8F, 8F, 8F, 8F});
            int a = 255;
            int r = 50 + random.nextInt(150);
            int g = 50 + random.nextInt(150);
            int b = 50 + random.nextInt(150);
            normalDrawable.setColor(Color.argb(a, r, g, b));

            // 设置按下的灰色背景
            GradientDrawable pressedDrawable = new GradientDrawable();
            pressedDrawable.setShape(GradientDrawable.RECTANGLE);
            pressedDrawable.setColor(Color.GRAY);
            pressedDrawable.setCornerRadii(new float[]{8F, 8F, 8F, 8F, 8F, 8F, 8F, 8F});
            // 背景选择器
            StateListDrawable stateDrawable = new StateListDrawable();
            stateDrawable.addState(new int[]{android.R.attr.state_pressed}, pressedDrawable);
            stateDrawable.addState(new int[]{}, normalDrawable);

            // 设置背景选择器到TextView上
            view.setBackground(stateDrawable);

            mFlowLayout.addView(view);
        }
    }

    @Override
    public void onEventMainThread(MainEvent event) {
        dealEvent(event);
    }

    /**
     * ViewPager适配器
     */
    private static class NotesPagerAdapter extends FragmentPagerAdapter {
        List<Fragment> mFragments = new ArrayList<>();

        public NotesPagerAdapter(FragmentManager fm, List<Fragment> fragments) {
            super(fm);
            this.mFragments.addAll(fragments);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }
    }

    /**
     * ViewPager监听器
     */
    private class NotesPagerChangeListener implements ViewPager.OnPageChangeListener {

        int pagerPosition = 0;

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            pagerPosition = position;

            switch (position) {
                case 0:

                    break;
                case 1:

                    break;
                default:
                    break;
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }
}
